package com.example.ovulationperiodtracker.utils;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.view.TabEntity;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

public class MainUiSetting {

    public static void setMainTab(ViewPager2 viewPager, CommonTabLayout tabLayout, List<Fragment> fragmentList, FragmentActivity fragmentActivity) {

        int[] iconSelectIds = {R.mipmap.ic_calendar1, R.mipmap.ic_char1, R.drawable.test_transparent, R.mipmap.ic_tj1, R.mipmap.ic_mine1};

        int[] iconUnselectIds = {R.mipmap.ic_calendar0, R.mipmap.ic_char0, R.drawable.test_transparent, R.mipmap.ic_tj0, R.mipmap.ic_mine0};
        String[] titles = {"日历", "图表", "", "统计", "我的"};

        viewPager.setAdapter(new FragmentStateAdapter(fragmentActivity) { // 使用 FragmentStateAdapter
            @Override
            public int getItemCount() {
                return fragmentList.size();
            }

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentList.get(position);
            }
        });

        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            tabEntities.add(new TabEntity(titles[i], iconSelectIds[i], iconUnselectIds[i]));
        }
        tabLayout.setTabData(tabEntities);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {
                // 如果需要处理重复点击同一个Tab的事件，可以在这里实现
            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }
        });


        viewPager.setUserInputEnabled(false);

    }

}
